<?php
##############################################
#
#   后台首页
# @ Time : 2017-1-1 - 下午7:37:36
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel;
use Think\Crypt\Driver\Base64;

class SystemController extends AdminBaseController {
	
	public function index(){
		//echo $_SERVER['DOCUMENT_ROOT'].__APP__;
		/* echo "<br>"."网站的根目录地址".__ROOT__." ";
		echo "<br>"."入口文件地址".__APP__." "; */
		$res = M('config')->where("inc_type='smtp' or inc_type='shop_info'")->select();
		if($res){
			foreach($res as $k=>$val){
				$config[$val['name']] = $val['value'];
			}
		}
		/* $editor = M('editor')->where("id = 10")->getField('text');
		$this->assign('editor', $editor); */
		//var_dump($config);
		$this->assign('config',$config);//当前配置项
		$this -> display('base_config');
	}
	
	public function setEditor(){
	    if(IS_GET){
	        $editor = M('editor')->where("id = 10")->getField('text');
	        $this->assign('editor', $editor);
	        $this->display('textarea');
	    }elseif(IS_POST){
	        $data['text'] = $_POST['text'];
	        $editor = M('editor'); 
	        $row =  $editor->add($data);
	        exit;
	        if($row !== false){
	            $this->success('操作成功', U('setEditor'), 1);
	        }else{
	            $this->error('操作失败');
	        }
	        /* showData($_FILES);exit;
	        var_dump($_POST);
	        if($_FILES['files']['name']){
	            $photo_img = AdminBaseModel::upload(200, 200, 'logo/');
	            $param['store_logo'] = $photo_img['store_logo'];  //serialize($photo_img)
	            if($param['store_logo']){
	                @unlink($_SERVER['DOCUMENT_ROOT'].substr(__APP__, 1).$param['old_store_logo']);
	            }
	        } */
	    }
	    
	}
	
	/* public function setConfig(){
		//$info = $this -> imgUpload(1,150,150);
		
	} */
	
	/**
	 * 配置邮箱
	 */
	public function setConfig(){
		$param = I('post.');
		$inc_type = $param['inc_type'];
		unset($param['inc_type']);
		
		if($_FILES['store_logo']['name']){
		    $photo_img = AdminBaseModel::upload(200, 200, 'logo/');
		    $param['store_logo'] = $photo_img['store_logo'];  //serialize($photo_img)
		    if($param['store_logo']){
		        @unlink($_SERVER['DOCUMENT_ROOT'].substr(__APP__, 1).$param['old_store_logo']);
		    }
		}
		unset($param['old_store_logo']);
		$result =  M('config')->where("inc_type='$inc_type'")->select();
		if($result){
			foreach($result as $val){
				$temp[$val['name']] = $val['value'];
			}
			foreach ($param as $k=>$v){
				$newArr = array('name'=>$k,'value'=>trim($v),'inc_type'=>$inc_type);
				if(!isset($temp[$k])){
					M('config')->add($newArr);//新key数据插入数据库
				}else{
					if($v!=$temp[$k])
						M('config')->where("name='$k'")->save($newArr);//缓存key存在且值有变更新此项
				}
			}
			/* //更新后的数据库记录
			$newRes =M('config')->where("inc_type='$inc_type'")->select();
			foreach ($newRes as $rs){
				$newData[$rs['name']] = $rs['value'];
			} */
	   }
	   $this->success('更新成功', U('index'), 1);
	}
	
	//发送测试邮件
	public function send_email(){
		$param = I('post.');
		//tpCache($param['inc_type'],$param);
		if(send_email($param['test_eamil'],'后台测试','测试发送验证码:'.mt_rand(1000,9999))){
			exit(json_encode(1));
		}else{
			exit(json_encode(0));
		}
	}
	
	public function upload(){    
		$upload = new \Think\Upload();// 实例化上传类    
		$upload->maxSize   =     3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型   
		$upload->savePath  =      'logos/'; // 设置附件上传目录    // 上传文件     
		$info   =   $upload->upload($_FILES);
		 
		 if(!$info) {// 上传错误提示错误信息        
		 	$this->error($upload->getError());    
		 }else{
		 	return $info;
		 }
	}
	
	public function imgUpload($path, $width, $height){
		$upload = new \Think\Upload();			// 实例化上传类
		$upload->maxSize   =     3145728 ;		// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =     'logos/'; 		// 设置附件上传目录    // 上传文件
		$upload->replace   =     true;			//覆盖同名文件
		$info   =   $upload->upload();
		if($info){
			
			$image = new \Think\Image();
			echo './Public/upload/'.$info['filedata_one']['savepath'].$info['filedata_one']['savename'];
			echo "<br/>";
			$image->open('./Public/upload/'.$info['filedata_one']['savepath'].$info['filedata_one']['savename']);
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			$image->thumb($width, $height)->save('./Public/upload/'.$info['filedata_one']['savepath'].time().rand(0,999).'.jpg');
			$image->thumb(50, 50)->save('./Public/upload/'.$info['filedata_one']['savepath'].time().rand(0,999).'.jpg');
			echo $_SERVER['DOCUMENT_ROOT'].__APP__.'/Public/upload/'.$info['filedata_one']['savepath'].$info['filedata_one']['savename'];
			unlink($_SERVER['DOCUMENT_ROOT'].__APP__.'/Public/upload/'.$info['filedata_one']['savepath'].$info['filedata_one']['savename']);
		}
		
		
		
	}

}